﻿using System;
using System.Diagnostics;

namespace ORM.Attribute
{
    [AttributeUsage(AttributeTargets.Property)]
    public class FieldAttribute: System.Attribute
    {
        private string _fieldName;
        private bool _isPrimary;
        private bool _isInsert = true;

        public virtual bool IsInsert
        {
            [DebuggerStepThrough]
            get { return _isInsert; }
            [DebuggerStepThrough]
            set { _isInsert = value; }
        }

        public virtual bool IsUpdate
        {
            [DebuggerStepThrough]
            get { return _isUpdate; }
            [DebuggerStepThrough]
            set { _isUpdate = value; }
        }

        private bool _isUpdate = true;
        public virtual bool IsPrimary
        {
            [DebuggerStepThrough]
            get { return _isPrimary; }
            [DebuggerStepThrough]
            set { _isPrimary = value; }
        }


        public FieldAttribute(){}

        public FieldAttribute(string fieldname, string description, DbFieldType fieldType, int max, int min)
        {
            _fieldName = fieldname;
            _fieldType = fieldType;
            _description = description;
            _max = max;
            _min = min;
        }

        public FieldAttribute(string fieldName, int min, int max)
        {
            _fieldName = fieldName;
            _fieldType = DbFieldType.VARCHAR;
            _description = "";
            _max = max;
            _min = min;
        }

        public FieldAttribute(string fieldName, DbFieldType fieldType)
        {
            _fieldName = fieldName;
            _fieldType = fieldType;
            _description = "";
            _max = Int32.MaxValue;
            _min = Int32.MinValue;
        }

        public virtual string FieldName
        {
            [DebuggerStepThrough]
            get { return _fieldName; }
            [DebuggerStepThrough]
            set { _fieldName = value; }
        }

        public virtual string Description
        {
            [DebuggerStepThrough]
            get { return _description; }
            [DebuggerStepThrough]
            set { _description = value; }
        }

        public virtual DbFieldType FieldType
        {
            [DebuggerStepThrough]
            get { return _fieldType; }
            [DebuggerStepThrough]
            set { _fieldType = value; }
        }

        public virtual string DefaultValue
        {
            [DebuggerStepThrough]
            get { return _defaultvalue; }
            [DebuggerStepThrough]
            set { _defaultvalue = value; }
        }

        public virtual int Max
        {
            [DebuggerStepThrough]
            get { return _max; }
            [DebuggerStepThrough]
            set { _max = value; }
        }

        public virtual int Min
        {
            [DebuggerStepThrough]
            get { return _min; }
            [DebuggerStepThrough]
            set { _min = value; }
        }

        private string _defaultvalue;
        private string _description;
        private DbFieldType _fieldType;
        private int _max;
        private int _min;
    }

    public enum DbFieldType
    {
        /// <summary>
        /// 对应数据库里的int类型
        /// </summary>
        INT = 0x0001,
        /// <summary>
        /// 对应tinyint类型
        /// </summary>
        TINYINT = 0x0002,
        /// <summary>
        /// 对应varchar类型
        /// </summary>
        VARCHAR = 0x0004,
        /// <summary>
        /// nvarchar类型
        /// </summary>
        NVARCHAR = 0x0008,
        /// <summary>
        /// datetime类型
        /// </summary>
        DATETIME = 0x0016,
        /// <summary>
        /// 对应bit类型
        /// </summary>
        BIT = 0x0032,
        TEXT = 0x0064,
        NTEXT = 0x0128,
        XML = 0x0256,
        MONEY = 0x0512,
        FLOAT = 0x1024,
        CHAR = 0x2048,
        NCHAR=0x4096
    }

    [Flags]
    public enum DbAction
    {
        insert = 0x0001,
        update = 0x0002,
        delete = 0x0004,
        select = 0x0008
    }
}
